package com.jiangjiang.eshop.membership.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.membership.dao.MemberPointMapper;
import com.jiangjiang.eshop.membership.domain.MemberPointEntity;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Objects;

/**
 * @ClassName MemberPointService
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/4 17:56
 * @Version 1.0
 **/
@Service
public class MemberPointService extends ServiceImpl<MemberPointMapper, MemberPointEntity> {

    /**
     * 根据用户账户获取用户等级
     *
     * @param accountId 用户账户id
     * @return 查询结果
     */
    public MemberPointEntity queryByAccountId(Long accountId) {
        LambdaQueryWrapper<MemberPointEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MemberPointEntity::getUserAccountId, accountId);
        MemberPointEntity entity = this.getOne(wrapper);
        if (Objects.isNull(entity)) {
            entity = new MemberPointEntity();
            entity.setUserAccountId(accountId);
            entity.setPoint(0L);
            this.save(entity);
        }
        return entity;
    }
}
